//
//  WelcomeViewController.swift
//  weibo
//
//  Created by cmh on 15/9/5.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    
    private var headerBottomLayOut: NSLayoutConstraint?

    override func viewDidLoad() {
        
        setUpUI()
        
        // 设置头像
        headerIconView.sd_setImageWithURL(UserAccountViewModel.shareUserAccount.headIconUrl)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        headerViewAnimation()
    }

    // 设置头像的动画
    private func headerViewAnimation() {
    
        // 计算高度
        let h = -(view.bounds.size.height + headerBottomLayOut!.constant)
        
        headerBottomLayOut?.constant = h
        
        textLabel.alpha = 0
        
        // 添加动画
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 更新控件的frame
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.textLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 发送切换控制器的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewContrillerNotification, object: nil)
                })
                
        }
        
    }
    
    // 设置UI
    private func setUpUI() {
        
        view.addSubview(iconView)
        view.addSubview(headerIconView)
        view.addSubview(textLabel)
        
        // 设置布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": iconView]))
        
        // 设置布局
        headerIconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: headerIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        headerBottomLayOut = NSLayoutConstraint(item: headerIconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200)
        view.addConstraint(self.headerBottomLayOut!)
        
        view.addConstraint(NSLayoutConstraint(item: headerIconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: headerIconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        
        // 设置文字的布局
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: headerIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: headerIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
    
    }
    
    // 懒加载背景图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 懒加载头像图片
    private lazy var headerIconView: UIImageView = {
        
        let headerView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角
        headerView.layer.cornerRadius = 45
        headerView.clipsToBounds = true
        
        return headerView
        
        }()
    
    // 懒加载文字
    private lazy var textLabel: UILabel = {
    
        let text = UILabel()
        text.text = "欢 迎 归 来"
        text.font = UIFont.systemFontOfSize(20)
        text.textColor = UIColor.darkGrayColor()
        text.sizeToFit()
        
        return text
        
    }()
    

}
